Micron Document
Deavmi's coding shack

Commit 6a63c4badaef8e8072a786b91e70e383b3bf5518


Parents : f1de9cc
Author : Tristan B. Kildaire <deavmi@redxen.eu>
Date : 2021-05-31T18:39:47+02:00

Added new journal entry

Changes

1 files changed, 130 insertions(+), 0 deletions(-)


Diff

diff --git a/new_docs/content/journal/struct-parsing.md b/new_docs/content/journal/struct-parsing.md
new file mode 100644
index 0000000..9dfd3d9
--- /dev/null
+++ b/new_docs/content/journal/struct-parsing.md
@@ -0,0 +1,130 @@
+---
+title: Added support for structs to parser
+author: Tristan B. Kildaire
+date: 2021-05-31
+tags: [parser, structs, internals]
+---
+
+## The parser update
+
+The parser now can parse structs such as:
+
+```
+struct structTest
+{
+ int j;
+ int j;
+
+ void pdsjhfjdsf(int j)
+ {
+
+ }
+
+ void pdsjhfjdsf(int j)
+ {
+
+ }
+}
+
+struct structTest2
+{
+
+}
+```
+
+And if it contains anything but variable declarations or function definitions then it will throw this eror:
+
+```
+[ERROR] Only function definitions and variable declarations allowed in struct body
+```
+
+If the struct only contains variable declarations or function definitions then everything will work out fine. However, for now, I have forbid the variable declarations to have assignments attached to them as well, if this occurs then you will get the following error:
+
+```
+[ERROR] Assignments not allowed in struct body
+```
+
+---
+
+## Generated structures
+
+Every pass made through each item in the struct's body returns a `Statement` object which is then checked to see if it is one of the beforementioned objects, is it a kind-of `Function` (function definition in T's D code) or `Variable` (variable declaration), when it is anything else it throws an error. For the `Variable` part we check if `variable.getAssignment()` is not `null`, which means an assignment is present, then we throw an error.
+
+The associated code is from the compiler codebase and is therefore licensed under the GPLv3.
+
+This piece of code already makes sure that only assignments, accessor statements and function definitions would be possible or the closing of the struct:
+
+From line TODO in commit ``
+
+```d
+/* If it is a type */
+if (symbolType == SymbolType.IDENT_TYPE)
+{
+ /* Might be a function, might be a variable, or assignment */
+ structMember = parseName();
+}
+/* If it is an accessor */
+else if (isAccessor(getCurrentToken()))
+{
+ structMember = parseAccessor();
+}
+/* If closing brace then exit */
+else if(symbolType == SymbolType.CCURLY)
+{
+ break;
+}
+```
+
+We don't have any `else` here because if it were anything else but the above the casting check below would help. We of course need to do the check we do with accessors as:
+
+The following should be legal:
+
+```
+struct Test
+{
+ public int j;
+}
+```
+
+However, this (also via an accessor) should not be legal:
+
+```
+struct Test
+{
+ public class j
+ {
+
+ }
+}
+```
+
+---
+
+This code is what makes sure that the rules are conformed to actually (the previous code segment was only realy to parse further or catch ther `}` which will be left behind after a `parseBody()` calls within the `parseAccessor()` and `parseName()` calls):
+
+```d
+/* Ensure only function declaration or variable declaration */
+if(cast(Function)structMember)
+{
+
+}
+else if(cast(Variable)structMember)
+{
+ /* Ensure that there is (WIP: for now) no assignment in the variable declaration */
+ Variable variableDeclaration = cast(Variable)structMember;
+
+ /* Raise error if an assignment is present */
+ if(variableDeclaration.getAssignment())
+ {
+ expect("Assignments not allowed in struct body");
+ }
+}
+/**
+* Anything else that isn't a assignment-less variable declaration
+* or a function definition is an error
+*/
+else
+{
+ expect("Only function definitions and variable declarations allowed in struct body");
+}
+```

Served by rngit 1.5.0 - Generated in 0.04s